package com.dd.cloud.user.service.chknowl.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.auth.entity.RoleMenu;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.chknowl.ChkonwlDoctorBookSetting;
import com.dd.cloud.user.mapper.chknowl.ChkonwlDoctorBookSettingMapper;
import com.dd.cloud.user.req.chknowl.ChknowlBookDoctorSettingReq;
import com.dd.cloud.user.req.chknowl.ChkonwlBookSettingReq;
import com.dd.cloud.user.service.chknowl.IChkonwlDoctorBookSettingService;
import com.dd.cloud.user.util.LoginUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.LoginContext;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 我的书籍设置 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-10-17
 */
@Service
public class ChkonwlDoctorBookSettingServiceImpl extends ServiceImpl<ChkonwlDoctorBookSettingMapper, ChkonwlDoctorBookSetting> implements IChkonwlDoctorBookSettingService {

    @Override
    public void addDoctorBookSetting(ChknowlBookDoctorSettingReq req) {
        ChkonwlDoctorBookSetting chkonwlDoctorBookSetting = new ChkonwlDoctorBookSetting();
        BeanUtil.copyProperties(req, chkonwlDoctorBookSetting);
        chkonwlDoctorBookSetting.setBookId(req.getBookId())
                .setStatus(1)
                .setCreateDate(LocalDateTime.now())
                .setDoctorId(req.getDoctorId());
        if (req.getType() == 2) {//保存记录
            ChkonwlDoctorBookSetting readRecord = this.queryReadBookReadRecord(req.getBookId());
            if (readRecord == null) {
                Boolean flag = this.save(chkonwlDoctorBookSetting);
                if (!flag) {
                    throw new ZekeException("保存书籍阅读记录失败");
                }
            } else {
                readRecord.setCreateDate(LocalDateTime.now());
                BeanUtil.copyProperties(req, readRecord);
                this.updateById(readRecord);
            }
        } else {
            Boolean flag = this.save(chkonwlDoctorBookSetting);
            if (!flag) {
                throw new ZekeException("保存书籍设置失败");
            }
        }
    }

    @Override
    public IPage<ChkonwlDoctorBookSetting> queryDoctorBookSettingPage(ChkonwlBookSettingReq req) {
        req.setDoctorId(req.getDoctorId());
        IPage<ChkonwlDoctorBookSetting> page = req.pagePojo();
        IPage<ChkonwlDoctorBookSetting> res = this.getBaseMapper().queryDoctorBookSettingPage(page, req);
        return res;
    }

    @Override
    public void addDoctorBookReadFirstRecord(Integer bookId) {

    }

    @Override
    public ChkonwlDoctorBookSetting queryReadBookReadRecord(Integer bookId) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        LambdaQueryWrapper<ChkonwlDoctorBookSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChkonwlDoctorBookSetting::getBookId, bookId)
                .eq(ChkonwlDoctorBookSetting::getDoctorId, loginInfo.getId())
                .eq(ChkonwlDoctorBookSetting::getType, 2)
                .eq(ChkonwlDoctorBookSetting::getStatus, 1)
                .orderByDesc(ChkonwlDoctorBookSetting::getCreateDate)
                .last("limit 1");
        return this.getOne(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBookSetting(Integer id) {
        ChkonwlDoctorBookSetting chkonwlDoctorBookSetting = this.getById(id);
        if (chkonwlDoctorBookSetting == null) {
            throw new ZekeException("书签不存在");
        }
        chkonwlDoctorBookSetting.setStatus(-1);
        boolean flag = this.updateById(chkonwlDoctorBookSetting);
        if (!flag) {
            throw new ZekeException("删除失败");
        }
    }

    @Override
    public boolean selectBookIsSetting(Integer bookId, Integer doctorId, String cfi) {
        LambdaQueryWrapper<ChkonwlDoctorBookSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChkonwlDoctorBookSetting::getBookId, bookId)
                .eq(ChkonwlDoctorBookSetting::getDoctorId, doctorId)
                .eq(ChkonwlDoctorBookSetting::getCfi, cfi)
                .eq(ChkonwlDoctorBookSetting::getStatus, 1)
                .eq(ChkonwlDoctorBookSetting::getType, 1);
        Integer count = this.count(wrapper);
        if (count > 0) {
            return true;
        }
        return false;
    }
}
